﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Mengsk.ProxyDelivery.Service
{
    public class PrintHistoryService : ServiceBase<PrintHistory>
    {

        private const string APIURL_INSERT = "/webservice/printhistory/insert.html";
        private const string APIURL_UPDATE = "/webservice/printhistory/update.html";
        private const string APIURL_DELETE = "/webservice/printhistory/delete.html";
        private const string APIURL_UPLOAD = "/webservice/printhistory/upload.html";
        private const string APIURL_MARKPOPDELIVERY = "/webservice/printhistory/markpopdelivery.html";

        public DataCollection<PrintHistory> GetByAll(long orderId, int sellerId, String deliveryCompany, string deliveryNumber, DateTime startTime, DateTime endTime, int pageIndex, int pageSize)
        {
            Request req = new Request { Api = "getByAll" };
            req.Add("orderId", orderId);
            req.Add("sellerId", sellerId);
            req.Add("deliveryCompany", deliveryCompany);
            req.Add("deliveryNumber", deliveryNumber);
            req.Add("startTime", this.FormatToDBTime(startTime));
            req.Add("endTime", this.FormatToDBTime(endTime));
            req.Add("pageIndex", pageIndex);
            req.Add("pageSize", pageSize);
            return this.DoRemoteCall<PrintHistory>(req).ToDataCollection();
        }

        public void Update(long id, string deliveryTemplateName, string deliveryCompany, string deliveryNumber)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("id", id.ToString());
            para.Add("deliveryTemplateName", deliveryTemplateName);
            para.Add("deliveryCompany", deliveryCompany);
            para.Add("deliveryNumber", deliveryNumber);
            this.DoPost<int>(APIURL_UPDATE, para);
        }

        public void Delete(long[] ids)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("ids", String.Join(",", ids));
            this.DoPost<int>(APIURL_DELETE, para);
        }

        public long Insert(PrintHistory printHistory)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();

            para.Add("orderId", printHistory.OrderId.ToString());
            para.Add("paperType", ((int)printHistory.PaperType).ToString());
            para.Add("wuliuId", printHistory.WuliuId ?? "");
            para.Add("sellerId", printHistory.SellerId.ToString());
            para.Add("deliveryTemplateName", printHistory.DeliveryTemplateName);
            para.Add("deliveryCompany", printHistory.DeliveryCompany);
            para.Add("deliveryNumber", printHistory.DeliveryNumber);
            para.Add("operator", printHistory.Operator);
            para.Add("popType", ((int)printHistory.PopType).ToString());
            para.Add("popBuyerId", printHistory.PopBuyerId.ToString());
            para.Add("popOrderMoney", printHistory.PopOrderMoney.ToString("F2"));
            para.Add("senderName", printHistory.SenderName);
            para.Add("senderPhone", printHistory.SenderPhone);
            para.Add("receiverMobile", printHistory.ReceiverMobile);
            para.Add("receiverPhone", printHistory.ReceiverPhone);
            para.Add("receiverAddress", printHistory.ReceiverAddress);
            para.Add("receiverName", printHistory.ReceiverName);
            para.Add("goodsInfo", printHistory.GoodsInfo);
            para.Add("giftInfo", printHistory.GiftInfo);

            var ret = this.DoPost<long>(APIURL_INSERT, para);
            if (ret.Datas.Count < 1)
            {
                throw new Exception("插入打印记录失败,没有返回值");
            }
            return ret.Datas[0];
        }

        public void Upload(long id)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("id", id.ToString());
            this.DoPost<int>(APIURL_UPLOAD, para);
        }

        public void MarkPopDelivery(long id)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("id", id.ToString());
            this.DoPost<int>(APIURL_MARKPOPDELIVERY, para);
        }

    }
}
